<?php

namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function login()
    {
//        is_login() && $this->redirect('/home/home/index');
        $this->display();
    }

    public function logout()
    {
        session('user_auth', null);
        session('user_role', null);
        redirect('/home/index/login');
        //$this->display();
    }

    public function doLogin($account, $password)
    {
        $User = D('User');
        $flag = $User->do_login($account, $password);
        if ($flag) {
            echo '1';
        } else {
            echo '0';
        }
        exit();
    }

    public function register()
    {
        $this->display();
    }

    public function doRegister()
    {
        $User = D('User');
        $data = $User->create();

        $exist = $User->find_account($data['account']);

        if ($exist != null) {
            echo '用户名已存在';
            exit();
        }

        $UserInfo = D('UserInfo');
        $data2 = $UserInfo->create();
        $data['user_info'] = $data2;

        if (!$data) { //数据对象创建错误
            echo '数据对象创建错误';
        } else {
            if (empty($data['id'])) {
                $data['create_at'] = time();
                $data['role'] = 3;
            }
            $result = $User->update($data, true);

            if (!$result) {
                echo $User->getError();
            } else {
                //---用户创建成功后把就先把旧任务全部下发
                $taskModel = D('Task');
                $UserTask = M('user_task');

                $dataList = [];
                $task_list = $taskModel->get_list(null, 0, 0, 1, 0, false);
                foreach ($task_list[0] as $task) {
                    $dataList[] = array('user_id' => $result, 'task_id' => $task['id']);
                }

                $task_list = $taskModel->get_list(null, 0, 1, 1, 0, false);
                foreach ($task_list[0] as $task) {
                    $dataList[] = array('user_id' => $result, 'task_id' => $task['id']);
                }
                $UserTask->addAll($dataList);

                //---用户创建成功后把就先把旧通知全部下发
                $noticeModel = D('Notice');
                $UserNotice = M('user_notice');

                $dataList = [];
                $notice_list = $noticeModel->get_list(null, 0, 1, 0, $data2['attribute'], $data2['rank']);
                foreach ($notice_list[0] as $notice) {
                    $dataList[] = array('user_id' => $result, 'notice_id' => $notice['id']);
                }
                $UserNotice->addAll($dataList);
                //---
                echo '0';
            }
        }
        exit();
    }

    public function update()
    {
        $UserModel = D('User');
        $user = $UserModel->get(session('user_auth')['id']);
        $this->assign('user', $user);
        $this->display();
    }

    public function doUpdate()
    {
        $UserMode = D('User');
        $id = $_POST['id'];
        $contact = $_POST['contact'];
        $contact_phone = $_POST['contact_phone'];
        $email = $_POST['email'];

        $password = $_POST['password'];
        $user = $UserMode->get($id);
        $user["user_info"]["contact"] = $contact;
        $user["user_info"]["contact_phone"] = $contact_phone;
        $user["user_info"]["email"] = $email;

        if (!empty($password)) {
            $user["password"] = $password;
        }

        $result = $UserMode->update($user, true);

        fc_clear_all();
        echo '0';
        exit();
    }

    public function resetpassword()
    {
        $UserModel = D('User');
        $user = $UserModel->get(session('user_auth')['id']);
        $this->assign('user', $user);
        $this->display();
    }

    public function doResetPassword()
    {
        $UserMode = D('User');
        $id = $_POST['id'];
        $sid = session('user_auth')['id'];
        if ($id != $sid) {
            echo '1';
            exit();
        }
        $password = $_POST['password'];

        $UserMode->where('id=%d', $id)->setField('password', $password);

        fc_clear_all();
        echo '0';
        exit();
    }

    public function clear()
    {
        fc_clear_all();
        echo 'all clear';
        exit();
    }

    public function reset()
    {
        fc_reset_temp_statistics();
        echo 'temp statistics reset';
        exit();
    }

    public function test3()
    {

//        $departs = fc_get_department_from_tree(2);

//        ($departs);
//        echo json_encode($departs);
        fc_set_temp_statistics(5, 'follow_count', 1, false);
        exit();
    }
}